# Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>

_realname=spice-gtk
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.41
pkgrel=3
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgdesc="GTK3 widget for SPICE clients (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-cyrus-sasl"
         "${MINGW_PACKAGE_PREFIX}-dbus-glib"
         "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
         "${MINGW_PACKAGE_PREFIX}-gstreamer"
         "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-phodav"
         "${MINGW_PACKAGE_PREFIX}-pixman"
         "${MINGW_PACKAGE_PREFIX}-spice-protocol"
         "${MINGW_PACKAGE_PREFIX}-usbredir"
         $([[ "${MSYSTEM}" == "CLANGARM64" ]] || echo "${MINGW_PACKAGE_PREFIX}-libcacard")
         "${MINGW_PACKAGE_PREFIX}-vala")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-pyparsing"
             "${MINGW_PACKAGE_PREFIX}-python-six")
license=("GPL2")
url="https://www.spice-space.org/"
source=(https://www.spice-space.org/download/gtk/${_realname}-${pkgver}.tar.xz{,.sig}
        001-fix-undefined-variables.patch
        002-export-symbols.patch
        003-fix-keymapgen-call.patch
        004-link-to-epoxy.patch)
sha256sums=('d8f8b5cbea9184702eeb8cc276a67d72acdb6e36e7c73349fb8445e5bca0969f'
            'SKIP'
            '33a1a6e5c9d58b20d736f69edff4c7973e057724b0b154619388c2e569aae018'
            '3295af8a5656adad24c2b3f8858d458597f14acb00bb5782412c2a115cacf507'
            '242e8184ab3f53bf895704276f6ffbfd7dab70aafe91f53c49db1cd39d085c64'
            'a56b0e489bc56ec1813d1eda6c6163814c4a5f1b4c919ef92c2d91a7c094ecf2')
validpgpkeys=('206D3B352F566F3B0E6572E997D9123DE37A484F') # Victor Toso de Carvalho <me@victortoso.com>

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  cp -p src/spice-glib-sym-file src/spice-glib-sym-file.def
  cp -p src/spice-gtk-sym-file src/spice-gtk-sym-file.def
  patch -p1 -i ${srcdir}/001-fix-undefined-variables.patch
  patch -p1 -i ${srcdir}/002-export-symbols.patch
  patch -p1 -i ${srcdir}/003-fix-keymapgen-call.patch
  patch -p1 -i ${srcdir}/004-link-to-epoxy.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if [[ "${MSYSTEM}" == "CLANGARM64" ]]; then
      extra_config+=("-Dsmartcard=disabled")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    --prefix="${MINGW_PREFIX}" \
    --buildtype=plain \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    -Dlibcap-ng=disabled \
    -Dpolkit=disabled \
    "${extra_config[@]}" \
    -Dspice-common:manual=false \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/meson.exe test || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install

  # Remove full path reference
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
